var base64 = require("../weui/example/images/base64");
//获取应用实例
const app = getApp()
Page({
  /** * 页面的初始数据 */
  data: {
    isActive: null,
    listMain: [
      { id: "1", region: "J", items: [{ id: "..", name: "金自爱", phone: "15958277217" }] }, 
      { id: "2", region: "Q", items: [{ id: "..", name: "钱健", phone: "13586594577" }] }, 
      { id: "3", region: "S", items: [{ id: "..", name: "邵亮", phone: "18606872989" }] }, 
      { id: "4", region: "Y", items: [{ id: "..", name: "杨道常", phone: "18658475789" }] }, 
      { id: "5", region: "Z", items: [{ id: "..", name: "张羽桦", phone: "13625849152" }] },],
    fixedTitle: null,
    toView: 'inTo0',
    oHeight: [],
    scroolHeight: 0,
    fixedTop: 0
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  //点击右侧字母导航定位触发
  scrollToViewFn: function (e) {
    var that = this; 
    var _id = e.target.dataset.id;
    for (var i = 0; i < that.data.listMain.length; ++i) {
      if (that.data.listMain[i].id === _id) {
        that.setData({
        isActive: _id, 
        toView: 'inTo' + _id, 
        fixedTitle: that.data.listMain[i].region
      })
      break;
      }
    }
  },
  // 页面滑动时触发
  onPageScroll: function (e) {
    this.setData({ scroolHeight: e.detail.scrollTop });
    for (let i in this.data.oHeight) {
      if (e.detail.scrollTop < this.data.oHeight[i].height) {
        this.setData({
          isActive: this.data.oHeight[i].key,
          fixedTitle: this.data.oHeight[i].name
        });
        return false;
      }
    }
  },
  // 处理数据格式，及获取分组高度
  getBrands: function () {
    var that = this; var number = 0
    //计算分组高度,wx.createSelectotQuery()获取节点信息
    for (let i = 0; i < that.data.listMain.length; ++i) {
      wx.createSelectorQuery().select('#inTo' + that.data.listMain[i].id).boundingClientRect(function (rect) {
        number = rect.height + number; 
        var newArry = [{ 'height': number, 'key': rect.dataset.id, "name": that.data.listMain[i].region }]
        that.setData({
          oHeight: that.data.oHeight.concat(newArry)
        })
      }).exec();
    };
  },
  onLoad: function (options) {
    var that = this;
    that.getBrands();
  },
  makePhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
    })
  }
})
